package com.esirong.whatime.model;

import android.content.Context;

import com.esirong.dao.DaoMaster;
import com.esirong.dao.DaoSession;
import com.esirong.dao.Task;
import com.esirong.dao.TaskDao;
import com.esirong.whatime.Application;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by esirong on 2015-04-20.
 */
public class TaskModel {

    public static void getCurrentTask(Context context, ResultHandler<Task> resultHandler){
        //获取一个当前的任务(与当前系统时间比较)
        DaoMaster daoMaster = Application.getInstance().getDaoMaster();
        DaoSession daoSession = daoMaster.newSession();
        TaskDao dao = daoSession.getTaskDao();
        //未完成的，开始时间小于当前系统时间
        QueryBuilder<Task> qb = dao.queryBuilder();
//        qb.where(TaskDao.Properties.Start_at.ge(System.currentTimeMillis())).build();

        List<Task> list = qb.list();
            if (list != null && !list.isEmpty()){
                for(Task info:list){
                    if(!info.getDone()){
                        resultHandler.onSuccess(info);
                        return;
                    }
                }

            } else {
            resultHandler.onFailed(-1);
        }

    }

    /**
     * 获得今天的任务（默认是全部类型）
     */
    public static void getTodayTasks(Context context, ResultHandler<List<Task>> resultHandler){
        //时间：当天零时，类型全部
        DaoMaster daoMaster = Application.getInstance().getDaoMaster();
        DaoSession daoSession = daoMaster.newSession();
        TaskDao dao = daoSession.getTaskDao();
        //未完成的，开始时间小于当前系统时间
        QueryBuilder<Task> qb = dao.queryBuilder();
        qb.where(TaskDao.Properties.Start_at.ge(System.currentTimeMillis()));
        qb.orderAsc(TaskDao.Properties.Start_at);
        List<Task> list = qb.list();
        if(list.isEmpty()){
            resultHandler.onFailed(-1);
        } else {
            resultHandler.onSuccess(list);
        }


    }

    /**
     * 添加一个新的Task
     * @param info
     * @param resultHandler
     */
    public static void addTask(Context context, Task info, ResultHandler<Task> resultHandler){
        DaoMaster daoMaster = Application.getInstance().getDaoMaster();
        DaoSession daoSession = daoMaster.newSession();
        TaskDao dao = daoSession.getTaskDao();

        long id;
        id =  dao.insertOrReplace(info);
        if (id != -1){
            info.setId(id);
            resultHandler.onSuccess(info);
        } else {
            resultHandler.onFailed(-1);
        }

    }

    public static void delTask(Task task ){
        DaoMaster daoMaster = Application.getInstance().getDaoMaster();
        DaoSession daoSession = daoMaster.newSession();
        TaskDao dao = daoSession.getTaskDao();
        dao.delete(task);
    }

    public static void delTask(long taskId){
        DaoMaster daoMaster = Application.getInstance().getDaoMaster();
        DaoSession daoSession = daoMaster.newSession();
        TaskDao dao = daoSession.getTaskDao();
        dao.deleteByKeyInTx(taskId);
    }

}
